//
//  YYAutoSliderViewController.m
//  YYSliderTabProject
//
//  Created by 蓓蕾 on 2021/7/16.
//

#import "YYAutoSliderViewController.h"
#import "DetailViewController.h"

/** 选中标题颜色 */
#define SelectTextColor [UIColor redColor]
/** 为选中标题颜色 */
#define NormalTextColor [UIColor blackColor]
/** 滑块颜色 */
#define SliderColor [UIColor redColor]
/** 标题个数 */
#define TitleCount 4

@interface YYAutoSliderViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *titleArr;// 标题数据

@property (nonatomic, strong) DetailViewController *WKPVC;
@property (nonatomic, strong) DetailViewController *SQZVC;
@property (nonatomic, strong) DetailViewController *YKPVC;
@property (nonatomic, strong) DetailViewController *YWCVC;


/** 标题视图 */
@property (nonatomic, strong) UIView *titleView;
/** 内容视图 */
@property (nonatomic, strong) UIScrollView *contentScrollView;
/** 指示条 */
@property (nonatomic, strong) UIView *sliderView;

@end

@implementation YYAutoSliderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUI];
}

-(void)setUI
{
    
    _contentScrollView = [[UIScrollView alloc] init];
    _contentScrollView.showsVerticalScrollIndicator = NO;
    _contentScrollView.showsHorizontalScrollIndicator = NO;
    _contentScrollView.delegate = self;
    _contentScrollView.pagingEnabled = YES;
    _contentScrollView.frame = CGRectMake(0, 100, kScreenWidth, kScreenHeight - 64);
    [self.view addSubview:self.contentScrollView];
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, kScreenWidth, 40)];
    [self.view addSubview:_titleView];
    _titleView.backgroundColor = [UIColor whiteColor];
    // 添加子控制器
    [self setupChildVc];
    // 添加标题
    [self setupTitle];
    
    
    // 默认显示第0个子控制器
    [self scrollViewDidEndScrollingAnimation:self.contentScrollView];
    
    
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = _currentIndex * self.contentScrollView.frame.size.width;
    [self.contentScrollView setContentOffset:offset animated:YES];
}

/**
 * 添加标题
 */
- (void)setupTitle
{
    // 定义临时变量
    NSArray *titles = @[@"未开票", @"申请中", @"已开票", @"已完成"];
    
    // 添加label
    for (NSInteger i = 0; i < titles.count; i++) {
        UILabel *label = [[UILabel alloc] init];
        
        label.text = titles[i];
        label.userInteractionEnabled = YES;
        label.frame = CGRectMake(i * (kScreenWidth / TitleCount), 0, kScreenWidth / TitleCount, CGRectGetHeight(_titleView.bounds));
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)]];
        label.tag = i;
        label.textAlignment = NSTextAlignmentCenter;
        [self.titleView addSubview:label];
        
    }
    self.contentScrollView.contentSize = CGSizeMake(titles.count * kScreenWidth, 0);
    
    // 底部的红色指示器
    _sliderView = [[UIView alloc] init];
    _sliderView.backgroundColor = SliderColor;
    [self.titleView addSubview:_sliderView];
}


/**
 * setupChildVc
 */
- (void)setupChildVc
{
    _WKPVC = [[DetailViewController alloc] init];
    _WKPVC.index = 1;
    [self addChildViewController:_WKPVC];
    
    _SQZVC = [[DetailViewController alloc] init];
    _WKPVC.index = 2;
    [self addChildViewController:_SQZVC];
    
    _YKPVC = [[DetailViewController alloc] init];
    _WKPVC.index = 3;
    [self addChildViewController:_YKPVC];
    
    _YWCVC = [[DetailViewController alloc] init];
    _YWCVC.index = 4;
    [self addChildViewController:_YWCVC];
    
    
}


/**
 * 监听顶部label点击
 */
- (void)labelClick:(UITapGestureRecognizer *)tap
{
    // 取出被点击label的索引
    NSInteger index = tap.view.tag;
    // 让底部的内容scrollView滚动到对应位置
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = index * self.contentScrollView.frame.size.width;
    [self.contentScrollView setContentOffset:offset animated:YES];
}


#pragma mark - <UIScrollViewDelegate>
/**
 * scrollView结束了滚动动画以后就会调用这个方法（比如- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;方法执行的动画完毕后）
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 一些临时变量
    CGFloat width = scrollView.frame.size.width;
    CGFloat height = scrollView.frame.size.height;
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 当前位置需要显示的控制器的索引
    NSInteger index = offsetX / width;
    
    // 让对应的顶部标题居中显示
    UILabel *label = self.titleView.subviews[index];
    label.textColor = SelectTextColor;
    //    [UIView animateWithDuration:0.25 animations:^{
    self.sliderView.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame), kScreenWidth / TitleCount, 2);
    //    }];
    // 让其他label回到最初的状态
    for (UILabel *otherLabel in self.titleView.subviews) {
        if (otherLabel != label) {
            if ([otherLabel isKindOfClass:[UILabel class]]) {
                otherLabel.textColor = NormalTextColor;
            }
        }
    }
    
    // 取出需要显示的控制器
    UIViewController *willShowVc = self.childViewControllers[index];
    
    // 如果当前位置的位置已经显示过了，就直接返回
    if ([willShowVc isViewLoaded]) return;
    
    // 添加控制器的view到contentScrollView中;
    willShowVc.view.frame = CGRectMake(offsetX, 0, width, height);
    [scrollView addSubview:willShowVc.view];
}

/**
 * 手指松开scrollView后，scrollView停止减速完毕就会调用这个
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

/**
 * 只要scrollView在滚动，就会调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat contentOffSetX = scrollView.contentOffset.x;
    CGFloat X = contentOffSetX * 1/TitleCount;
    CGRect frame = _sliderView.frame;
    frame.origin.x = X;
    _sliderView.frame = frame;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
